<form class="layui-form seller-form" action="">
	<div class="layui-form-item">
		<div class="layui-inline">
			<button class="layui-btn add-class layui-btn-sm"><i class="layui-icon">&#xe608;</i>添加</button>
		</div>
	</div>
</form>

<div class="table-body">
	<table id="typeTable" lay-filter="test"></table>
</div>

<script>
    var table;
    layui.use(['form', 'layedit', 'laydate','table'], function(){
        table = layui.table.render({
            elem: '#typeTable',
            height: 'full-330',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            url: "{:url('GoodsTypeSpec/index')}?_ajax=1",
            id: 'typeTable',
            cols: [[
                {type:'numbers'},
                {field: 'name', width: 100, title: '名称'},
                {field: 'spec_value', width: 200, title: '属性值',templet:function(data){
                    var html="";
                    if(data.spec_value.length>0){
                        $.each(data.spec_value,function(i,j){
                            html = html + "|"+ j.value;
                        });
                        html=html.substr(1);
                        return html;
                    }else{
                        return '无属性值';
                    }
                }},
                {field: 'sort', width: 100, title: '排序'},
                {field: 'operating', title: '操作', templet: function (data) {
                    var html = '<a  class="layui-btn layui-btn-xs edit-class" data-id="' + data.id + '">编辑</a>';
                    html += '<a class="layui-btn layui-btn-danger layui-btn-xs del-class" data-name="'+data.name+'" data-id="' + data.id + '">删除</a>';
                    return html;
                }}
            ]],
        });

        layui.form.on('submit(*)', function(data){
            layui.table.reload('userTable', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //添加属性
        $(document).on('click', '.add-class', function(){
            var id = $(this).attr('data-id');
            JsGet('{:url("GoodsTypeSpec/add")}',function (e) {
                window_box = layer.open({
                    type: 1,
                    title: '添加属性',
                    area: ['420px', '420px'], //宽高
                    content: e
                });
            });
            return false;
        });

        //编辑属性
        $(document).on('click', '.edit-class', function(){
            var id = $(this).attr('data-id');
            $.ajax({
                url: '{:url("GoodsTypeSpec/edit")}',
                type: 'get',
                data: {
                    'id': id
                },
                success: function(e){
                    window_box = layer.open({
                        type: 1,
                        title: '编辑属性',
                        area: ['420px', '550px'], //宽高
                        content: e
                    });
                }
            });
            return false;
        });


        //删除分类
        $(document).on('click', '.del-class', function(){
            var id = $(this).attr('data-id');
            var name = $(this).attr('data-name');
            layer.confirm('确认删除属性：'+name+' 吗？', {
                title: '提示', btn: ['确认', '取消'] //按钮
            }, function(){
                delClass(id);
            });
        });

        //删除操作
        function delClass(id){
            $.ajax({
                url: '{:url("GoodsTypeSpec/del")}',
                type: 'post',
                data: {
                    'id': id
                },
                success: function(e){
                    layer.msg(e.msg, {time: 1300}, function(){
                        table.reload('typeTable');
                    });
                }
            });
        }

    });

    function saveSpec(data){
        if(!data){
            layer.msg('请先完善数据', {time: 1300});
            return false;
        }
        $.ajax({
            url: '{:url("GoodsTypeSpec/add")}',
            type: 'post',
            data: data,
            dataType: 'json',
            success: function(e){
                if(e.status === true){
                    layer.close(window_box);
                    layer.msg(e.msg, {time: 1300}, function(){
                        table.reload('typeTable');
                    });
                }else{
                    layer.msg(e.msg, {time: 1300});
                }
            }
        })
    }

    function editSpec(data){
        if(!data){
            layer.msg('请先完善数据', {time: 1300});
            return false;
        }
        $.ajax({
            url: '{:url("GoodsTypeSpec/edit")}',
            type: 'post',
            data: data,
            dataType: 'json',
            success: function(e){
                if(e.status === true){
                    layer.close(window_box);
                    layer.msg(e.msg, {time: 1300}, function(){
                        table.reload('typeTable');
                    });
                }else{
                    layer.msg(e.msg, {time: 1300});
                }
            }
        })
    }
</script>
